import sqlite3

# 创建数据库连接
conn = sqlite3.connect('student.db')
cursor = conn.cursor()

# 创建学生表
cursor.execute('''
CREATE TABLE IF NOT EXISTS student (
    name TEXT PRIMARY KEY,
    age INTEGER,
    address TEXT,
    phone_number TEXT
)
''')

def add_student(name, age, address, phone_number):
    cursor.execute("INSERT INTO student (name, age, address, phone_number) VALUES (?, ?, ?, ?)", (name, age, address, phone_number))
    conn.commit()
    print(f"{name}存入成功")

def update_student(name, age=None, address=None, phone_number=None):
    if age:
        cursor.execute("UPDATE student SET age = ? WHERE name = ?", (age, name))
    if address:
        cursor.execute("UPDATE student SET address = ? WHERE name = ?", (address, name))
    if phone_number:
        cursor.execute("UPDATE student SET phone_number = ? WHERE name = ?", (phone_number, name))
    conn.commit()

def delete_student(name):
    cursor.execute("DELETE FROM student WHERE name = ?", (name,))
    conn.commit()

def show_all_students():
    cursor.execute("SELECT * FROM student")
    students = cursor.fetchall()
    for student in students:
        print(student[0])

def random_student():
    cursor.execute("SELECT * FROM student")
    students = cursor.fetchall()
    import random
    random_student = random.choice(students)
    print(f"{random_student[0]}到！")

while True:
    operation = input("请输入操作（1-存入学生，2-更改学生，3-删除学生，4-展示全部学生，5-随机抽取学生）：")
    if operation == '1':
        name = input("请输入姓名：")
        age = int(input("请输入年龄："))
        address = input("请输入家庭地址：")
        phone_number = input("请输入手机号：")
        add_student(name, age, address, phone_number)
    elif operation == '2':
        name = input("请输入姓名：")
        age = input("请输入新的年龄（留空不修改）：")
        address = input("请输入新的家庭地址（留空不修改）：")
        phone_number = input("请输入新的手机号（留空不修改）：")
        update_student(name, age, address, phone_number)
    elif operation == '3':
        name = input("请输入要删除的学生姓名：")
        delete_student(name)
    elif operation == '4':
        show_all_students()
    elif operation == '5':
        random_student()
    else:
        print("无效的操作，请重新输入")
